home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_03_02 / 3n02012a < prev    next >
Text File  |  1991-12-13  |  2KB  |  91 lines

  1. #include <stdlib.h>
  2. #include <assert.h>
  3. #include <stdio.h>
  4. #include <time.h>
  5. #include "challoc.h"
  6.  
  7. int Random(int num)
  8.     {
  9.     int     Result  = int(long(rand())*num/(RAND_MAX+1));
  10.     return Result < 0 ? -Result : Result;
  11.     }
  12.  
  13. class   IntSet : public ChunkAllocated
  14.     {
  15. public:
  16.     IntSet();
  17.     int     Foo[4];
  18.     };
  19. IntSet::IntSet()
  20.     {
  21.     Foo[0] = Foo[1] = Foo[2] = Foo[3] = 0;
  22.     };
  23.  
  24. class   IntSet2
  25.     {
  26. public:
  27.     IntSet2();
  28.     int     Foo[4];
  29.     };
  30. IntSet2::IntSet2()
  31.     {
  32.     Foo[0] = Foo[1] = Foo[2] = Foo[3] = 0;
  33.     };
  34.  
  35. const   int MAXPOINTERS     = 1024;
  36. const   int MAXITERATIONS   = 2048;
  37.  
  38. IntSet  *Pointers[MAXPOINTERS];
  39. IntSet2 *Pointers2[MAXPOINTERS];
  40.  
  41. int     main(int /*argc*/, char **/*argv*/)
  42.     {
  43.     int i, j, k;
  44.     printf("Begin Benchmark\n");
  45.     time_t StartTime   = time(NULL);
  46.     for(j = 0; j < MAXITERATIONS; ++j)
  47.         for(i = 0; i < MAXPOINTERS; ++i)
  48.             if(Pointers[k=Random(MAXPOINTERS)])
  49.                 Pointers[k] = NULL;
  50.     float   EmptyTime   = difftime(time(NULL), StartTime);
  51.     printf( "empty loop time is %5.2f seconds\n", EmptyTime);
  52.  
  53.     StartTime   = time(NULL);
  54.     for(j = 0; j < MAXITERATIONS; ++j)
  55.         for(i = 0; i < MAXPOINTERS; ++i)
  56.             if(Pointers2[k=Random(MAXPOINTERS)])
  57.                 {
  58.                 delete Pointers2[k];
  59.                 Pointers2[k] = NULL;
  60.                 }
  61.             else
  62.                 {
  63.                 Pointers2[k] = new IntSet2;
  64.                 assert(Pointers2[k] != NULL);
  65.                 }
  66.     float StandardTime = difftime(time(NULL), StartTime);
  67.     printf( "Standard allocation time %5.2f seconds\n", StandardTime);
  68.  
  69.     StartTime   = time(NULL);
  70.     for(j = 0; j < MAXITERATIONS; ++j)
  71.         for(i = 0; i < MAXPOINTERS; ++i)
  72.             if(Pointers[k=Random(MAXPOINTERS)])
  73.                 {
  74.                 delete Pointers[k];
  75.                 Pointers[k] = NULL;
  76.                 }
  77.             else
  78.                 {
  79.                 Pointers[k] = new IntSet;
  80.                 assert(Pointers[k] != NULL);
  81.                 }
  82.     float ChunkTime = difftime(time(NULL), StartTime);
  83.     printf( "Chunk allocation time %5.2f seconds\n", ChunkTime);
  84.  
  85.     printf( "Improvement = %5.2f %%\n", 100.0
  86.         * ((StandardTime-EmptyTime) - (ChunkTime-EmptyTime))
  87.         / (StandardTime-EmptyTime));
  88.     return 0;
  89.     }
  90.  
  91.